/*
 * This file is part of the OdinMS Maple Story Server
 * Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
 *                    Matthias Butz <matze@odinms.de>
 *                    Jan Christian Meyer <vimes@odinms.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License version 3
 * as published by the Free Software Foundation. You may not use, modify
 * or distribute this program under any other version of the
 * GNU Affero General Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
-- Odin JavaScript --------------------------------------------------------------------------------
	Cabin between Orbis and Leafre
-- By ---------------------------------------------------------------------------------------------
	Information
-- Version Info -----------------------------------------------------------------------------------
	1.8 - Fixed packets, and timing [GoldenKevin]
	    - Code cleanup, replaced redundants, and added comments [GoldenKevin]
	    - Reordered statements to be grouped with similar functions
	1.7 - Correct travel time [Information]
	1.6 - Relocate Import to new source [GoldenKevin] 
	1.5 - Fix for infinity looping [Information]
	1.4 - Ship/boat is now showed
	    - Removed temp message[Information]
	    - Credit to Snow/superraz777 for old source
	    - Credit to Titan/Kool for the ship/boat packet
	1.3 - Removing some function since is not needed [Information]
	    - Remove register player menthod [Information]
	1.2 - It should be 2 ships not 1 [Information]
	1.1 - Add timer variable for easy edit [Information]
	1.0 - First Version by Information
---------------------------------------------------------------------------------------------------
**/

importPackage(net.sf.odinms.tools);

//Time Setting is in millisecond
var closeTime = 240000; //The time to close the gate
var beginTime = 300000; //The time to begin the ride
var rideTime = 300000; //The time that require move to destination
var Orbis_btf;
var Cabin_to_Orbis;
var Orbis_docked;
var Leafre_btf;
var Cabin_to_Leafre;
var Leafre_docked;

function init() {
	//Set the maps upon starting the instance
	Orbis_btf = em.getChannelServer().getMapFactory().getMap(200000132);
	Leafre_btf = em.getChannelServer().getMapFactory().getMap(240000111);
	Cabin_to_Orbis = em.getChannelServer().getMapFactory().getMap(200090210);
	Cabin_to_Leafre = em.getChannelServer().getMapFactory().getMap(200090200);
	Orbis_docked = em.getChannelServer().getMapFactory().getMap(200000100);
	Leafre_docked = em.getChannelServer().getMapFactory().getMap(240000100);
	Orbis_Station = em.getChannelServer().getMapFactory().getMap(200000131);
	Leafre_Station = em.getChannelServer().getMapFactory().getMap(240000110);
	//Get current time so that we can determine whether the boat is docked or when the next boat will come
	var cTime = new Date();
	var m = cTime.getMinutes();
	var s = cTime.getSeconds();
	//Finds the remainder of the current minute of the hour divided by 10 (boat resets every 10 minutes)
	m %= 10;
	if(m >= 5 && m <= 9) {
		//Docked from 5 - 9:59 minutes, from 5 - 9 minutes past last departure, you can board
		//Dock boats because they haven't left yet
		Leafre_docked.setDocked(true);
		Orbis_Station.setDocked(true);
		em.setProperty("docked", "true");
		m -= rideTime/60000; // m = m - (rideTime / 60000) Minutes since the boat arrived (min - 5), i.e : If it's 8 minutes past last departure, 3 minutes since boat arrived, etc.)
		//During the last minute before takeoff, entry is forbidden (4 (9) is the last minute, since it takes off at 5 (10) or (0).).
		if(m != 4) {
			//If it's 5 - 9:59 minutes after last departure / 0 - 3:59 minutes since boat arrived, you can board
			var closeDoorsIn = closeTime - ((m * 60000) + (s * 1000));
			em.setProperty("entry", "true");
			em.schedule("stopEntry", closeDoorsIn);
		} else {
			//If it's past 9 - 9:59 minutes / 4 - 4:59 minutes since boat arrived, it's still docked, but you can't board
			em.setProperty("entry", "false");
		}
		//Takeoff time (i.e. : if beginTime == 5 minutes and is currently 3:40 minutes since boat arrived, takeoff in 1:20 minutes
		m = beginTime - ((m * 60000) + (s * 1000));
		em.schedule("takeoff", m);
	} else {
		//If 0 - 4:59 minutes past last departure, boat is undocked, you have to wait til next boat comes.
		//Time when next boat arrives (i.e. : if rideTime == 5 minutes, and is currently 4 minutes since last boat departed, next boat arrives in 1 minute.
		m = rideTime - ((m * 60000) + (s * 1000));
		//Undocked, so set dock to false
		Leafre_docked.setDocked(false);
		Orbis_Station.setDocked(false);
		em.setProperty("docked", "false");
		//Undocked, so of course you can't enter
		em.setProperty("entry", "false");
		//Automatically send the next boat when the time comes
		em.schedule("scheduleNew", m);
	} 
}

function scheduleNew() {
	//Ship has docked (during arrival from a previous ship or for the first time after the server started)
	Leafre_Station.setDocked(true);
	Orbis_Station.setDocked(true);
	Leafre_Station.broadcastMessage(MaplePacketCreator.showBoatEffect(1548));
	Orbis_Station.broadcastMessage(MaplePacketCreator.showBoatEffect(1548));
	em.setProperty("docked", "true");
	em.setProperty("entry", "true");
	//Automatically close doors after 4 minutes.
	em.schedule("stopEntry", closeTime);
	//Take off after 5 minutes, 1 minute after doors have closed
	em.schedule("takeoff", beginTime);
}

function stopEntry() {
	//Ship is closing it's doors, 1 minute before departure
	em.setProperty("entry","false");
}

function takeoff() {
	//Ship is taking off, 5 minutes after arriving and boarding, 1 minute after doors are closed, 10 minutes after previous departure
	//Warp all players in the Orbis to Leafre waiting map to the boat
	var waitingCharacters = Orbis_btf.getCharacters().iterator();
	var chr;
	while(waitingCharacters.hasNext()) {
		chr = waitingCharacters.next();
		chr.changeMap(Cabin_to_Leafre, Cabin_to_Leafre.getPortal(0));
	}
	//Warp all players in the Leafre to Orbis waiting map to the boat
	waitingCharacters = Leafre_btf.getCharacters().iterator();
	while(waitingCharacters.hasNext()) {
		chr = waitingCharacters.next();
		chr.changeMap(Cabin_to_Orbis, Cabin_to_Orbis.getPortal(0));
	}
	//Undock the boat
	em.setProperty("docked","false");
	Leafre_Station.setDocked(false);
	Orbis_Station.setDocked(false);
	Leafre_Station.broadcastMessage(MaplePacketCreator.showBoatEffect(520));
	Orbis_Station.broadcastMessage(MaplePacketCreator.showBoatEffect(520));
	//Schedule time to arrival (10 minutes after undocking)
	em.schedule("arrived", rideTime);
}

function arrived() {
	//Ship has arrived in Orbis or Leafre, 5 minutes after departure, 10 minutes after boarding started, 10 minutes after previous arrival
	//Warp all players in the Orbis Boat to the Orbis Ticketing Place
	var waitingCharacters = Cabin_to_Orbis.getCharacters().iterator();
	var chr;
	while(waitingCharacters.hasNext()) {
		chr = waitingCharacters.next();
		chr.changeMap(Orbis_docked, Orbis_docked.getPortal(0));
	}
	//Warp all players in the Leafre Boat to the Leafre Station
	waitingCharacters = Cabin_to_Leafre.getCharacters().iterator();
	while(waitingCharacters.hasNext()) {
		chr = waitingCharacters.next();
		chr.changeMap(Leafre_docked, Leafre_docked.getPortal(0));
	}
	//Dock ship and set a timer to depart again in 5 minutes.
	scheduleNew();
}

function cancelSchedule() {
}